fix(autograd): include traced keys in HDF5 hash input #2875
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
__tidy3d_traced_field_keys__
) so cache hashes differ when the tracked set changesTracer
,FieldMap
, andTracerKeys
helpers intotidy3d/components/autograd
and reuse them from both the serializer and the web pipelineAlternatives Considered
TracerKeys
file: this leaves the cache blind to tracer changes, because the hash sees identical HDF5 exportsmodel.attrs
: those attrs are user-controlled metadata and cached JSON already assumes they are non-functionalRationale
Writing the traced-key summary to an HDF5 attribute keeps the metadata with the exported simulation, feeds into the hashing logic, and avoids making autograd bookkeeping part of the user-facing schema.
Greptile Overview
Updated On: 2025-10-08 08:26:25 UTC
Summary
This PR fixes a critical autograd cache bug where simulations with identical static parameters but different traced field sets would incorrectly share cache entries. The core issue was that the cache hash only considered the HDF5 file contents (which are identical for the same static simulation) but ignored the sidecar `TracerKeys` file that tracks which fields are being traced by autograd.The solution serializes traced field keys directly into the HDF5 metadata using a new attribute
__tidy3d_traced_field_keys__
. This ensures that simulations tracking different autograd parameters generate different cache hashes, preventing incorrect cache hits that could lead to wrong gradient computations in optimization workflows.The implementation includes a significant refactoring that moves the
Tracer
,FieldMap
, andTracerKeys
helper classes fromtidy3d/web/api/autograd/utils.py
into a new shared moduletidy3d/components/autograd/field_map.py
. This centralization enables code reuse between the web pipeline and the serialization components while maintaining consistency in traced field metadata handling.A new regression test
test_sim_hash_changes_with_traced_keys()
validates that when the traced field set changes (by converting a structure to static), both the field map and the simulation hash change accordingly, ensuring proper cache invalidation.Important Files Changed
Changed Files
Confidence score: 4/5
Sequence Diagram